package com.bn.feeds.tools;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bn.feedMaster.FeedProcessException;
import com.bn.feeds.MasterFeedConfiguration;

import atg.core.util.StringUtils;
import atg.repository.MutableRepository;
import atg.repository.MutableRepositoryItem;
import atg.repository.Repository;
import atg.repository.RepositoryException;
import atg.repository.RepositoryItem;

/**
 * The Class DepartementRepositoryTools.
 * @author Abhishek
 */
public class DepartmentRepositoryTools extends BaseRepositoryTools {
	
	/** The repository. */
	private Repository repository;
	
	/**
	 * Adds/updates a repository item with the data provided
	 * 
	 * @param config
	 *            configuration for the Feed
	 * @param itemList
	 *            one Row data
	 * @param header
	 *            the feed file header
	 * @param idColumnIndex
	 *            index of id column property in data row(itemList)
	 * @param repositoryPropertiesToTokenIndexMap
	 *            a map which contains repository properties(primitive type) index in data row(which property has value at what index in data row)
	 * @param repositoryItemPropertiesToTokenIndexMap
	 *            a map which contains repository properties(RepositoryItem type) index in data row(which property has value at what index in data row)
	 * @param productProcessedMap
	 *            a map which contains id's of all the items which are processed so far in the feed execution
	 * @return 
	 * @throws FeedProcessException
	 *             the feed process exception
	 */
	public List<String> processRecord(MasterFeedConfiguration config, String[] dataRow, String[] header, int idColumnIndex, Map<String, Integer> repositoryPropertiesToTokenIndexMap, Map<String, Integer> repositoryItemPropertiesToTokenIndexMap,
            Map<String, String> productProcessedMap, Map<String, Integer> mandatoryPropertiesMap) throws FeedProcessException {
        if (isLoggingDebug()) {
            logDebug("ProductRepositoryTools::>processRecord()::BEGIN");
        }
        List<String> errorList = new ArrayList<String>();
        if(!validateMandatoryProperties(config, mandatoryPropertiesMap, dataRow, errorList)){
            return errorList;
            }
        MutableRepository mutableRespository = config.getRepository();
        Repository repository = config.getPrdRepository();
        String itemDescriptorName = config.getItemDescriptorName();
        String deptItemId = dataRow[idColumnIndex];
        if (!StringUtils.isBlank(deptItemId)) {
            try {
            	deptItemId = deptItemId.trim();
                RepositoryItem deptItem = null;
                MutableRepositoryItem mutableDepartmentItem = null;
                
                deptItem = repository.getItem(deptItemId, itemDescriptorName);
                if (deptItem != null) {
                    if (isLoggingDebug()) {
                        logDebug("DepartmentRepositoryTools::>processRecord()::Department item with id:" + deptItem.getRepositoryId() + " already Exists. Updating Item");
                    }
                    mutableDepartmentItem = mutableRespository.getItemForUpdate(deptItemId, itemDescriptorName);
                    setPropertiesToItem(mutableRespository, repository, mutableDepartmentItem, repositoryPropertiesToTokenIndexMap, repositoryItemPropertiesToTokenIndexMap, dataRow, itemDescriptorName, errorList);
                    mutableRespository.updateItem(mutableDepartmentItem);
                } else {
                    if (isLoggingDebug()) {
                        logDebug("DepartmentRepositoryTools::>processRecord()::Department item with id:" + deptItemId + " does not Exists. Creating new Item");
                    }
                    mutableDepartmentItem = mutableRespository.createItem(deptItemId, itemDescriptorName);
                    setPropertiesToItem(mutableRespository, repository, mutableDepartmentItem, repositoryPropertiesToTokenIndexMap, repositoryItemPropertiesToTokenIndexMap, dataRow, itemDescriptorName, errorList);
                    mutableRespository.addItem(mutableDepartmentItem);
                }
            } catch (RepositoryException e) {
            	logError(e);
                throw new FeedProcessException(e);
            } catch (NumberFormatException e) {
            	logError(e);
                throw new FeedProcessException(e);
            }
        } else {

            if (isLoggingDebug()) {
                logDebug("DepartmentRepositoryTools::>processRecord()::Value of Id property in feed is null");
            }
            throw new FeedProcessException("Value of Id property in feed is null");

        }
        if (isLoggingDebug()) {
            logDebug("DepartmentRepositoryTools::>processRecord()::END");
        }
        return errorList;
    }
	
	/**
	 * Gets the repository.
	 * 
	 * @return the repository
	 */
	public Repository getRepository() {
		return repository;
	}

	/**
	 * Sets the repository.
	 * 
	 * @param repository
	 *            the new repository
	 */
	public void setRepository(Repository repository) {
		this.repository = repository;
	}
	

	@Override
	public boolean cleanUpData(MutableRepository repository,
			String itemDescriptor) throws FeedProcessException {
		// TODO Auto-generated method stub
		return false;
	}	
}
